﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AutoWindowsSize; //窗口自适应
namespace h1
{
    public partial class Form_Choose : Form
    {
        int fTabel;
        Form_data form;
        AutoAdaptWindowsSize AutoSize; //窗口自适应
        public Form_Choose()
        {
            InitializeComponent();
        }
        public Form_Choose(Form_data f,int tabel)
        {
            InitializeComponent();
            string connString = "data source=(LocalDb)\\MSSQLLocalDB;initial catalog=h1.Sqlite;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";
            using (SqlConnection connection = new SqlConnection(connString))
            {
                string sql = "";
                fTabel = tabel;
                form = f;
                switch (tabel)
                {
                    case 0:
                        sql = "SELECT * FROM Schools";
                        break;
                    case 1:
                        sql = "SELECT * FROM Classes";
                        break;
                    case 2:
                        sql = "SELECT * FROM Students";
                        break;
                }
                connection.Open();
                using (SqlDataAdapter sda = new SqlDataAdapter(sql, connection))
                {
                    try
                    {
                        DataSet ds = new DataSet();
                        sda.Fill(ds);
                        foreach(DataColumn dr in ds.Tables[0].Columns)
                        {
                            comboBox1.Items.Add(dr.ColumnName);
                        }
                        comboBox2.Items.Add("等于");
                    }
                    catch (Exception ex)
                    {
                        Form_message fm = new Form_message("数据库数据查找出错", "出错描述：" + ex.Message);
                        fm.ShowDialog();
                    }
                }
            }
        }
        private void Form_Choose_Load(object sender, EventArgs e)
        {
            AutoSize = new AutoAdaptWindowsSize(this);//窗口自适应
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "")
            {
                errorProvider1.SetError(comboBox1,"请选择查找对象");
            }
            else if(comboBox2.Text == "")
            {
                errorProvider1.SetError(comboBox2, "请选择匹配方式");
            }
            /*else if (comboBox3.Text == "")
            {
                errorProvider1.SetError(comboBox3, "请选择排序方式");
            }*/
            else if (textBox1   .Text == "")
            {
                errorProvider1.SetError(textBox1, "请选择匹配数值");
            }
            else
            {
                errorProvider1.Clear();
                string connString = "data source=(LocalDb)\\MSSQLLocalDB;initial catalog=h1.Sqlite;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";
                using (SqlConnection connection = new SqlConnection(connString))
                {
                    string sql = "";
                    string type = "";
                    switch (fTabel)
                    {
                        case 0:
                            sql = "SELECT * FROM Schools";
                            type = "School";
                            break;
                        case 1:
                            sql = "SELECT * FROM Classes";
                            type = "Class";
                            break;
                        case 2:
                            sql = "SELECT * FROM Students";
                            type = "Student";
                            break;
                    }
                    connection.Open();
                    using (SqlDataAdapter sda = new SqlDataAdapter(sql, connection))
                    {
                        try
                        {
                            DataSet ds = new DataSet();
                            sda.Fill(ds);
                            string op="";
                            switch (comboBox2.Text)
                            {
                                case "等于":
                                    op = "=";
                                    break;
                                case "大于":
                                    op = ">";
                                    break;
                                case "小于":
                                    op = "<";
                                    break;
                                default:
                                    op = "=";
                                    break;
                            }
                            string orderstring = "";
                            if (comboBox3.Text == "升序")
                            {
                                orderstring = comboBox1.Text + " asc";
                            }
                            else if (comboBox3.Text == "降序")
                            {
                                orderstring = comboBox1.Text + " desc";
                            }
                            else
                            {
                                orderstring = comboBox1.Text + " asc";
                            }
                            DataRow[] rows = ds.Tables[0].Select($"{comboBox1.Text}{op}'{textBox1.Text}'",orderstring);
                            /*DataRow[] newrows=new DataRow[rows.Length];
                            for (int i = 0; i < rows.Length; i++)
                            {
                                DataRow row = ds.Tables[0].NewRow();
                                for(int j = 0; j < rows[i].ItemArray.Length; j++)
                                {
                                    if (rows[i][j]!=null)
                                    row[j] = rows[i][j];
                                }
                                newrows[i] = row;
                            }
                            /*ds.Tables[0].Rows.Clear();
                            for (int i = 0; i < newrows.Length; i++)
                            {
                                //Console.WriteLine(newrows.Length);
                                //Console.WriteLine("row "+i);
                                //Console.WriteLine(newrows[i].ToString());
                                //Console.WriteLine(newrows[i][0]+","+newrows[i][1]);
                                ds.Tables[0].Rows.Add(newrows[i]);
                            }*/
                            List<string> ids=new List<string>();
                            foreach (DataRow row in rows)
                            {
                                ids.Add((string)row[type+"ID"]);
                            }
                            form.show(ids);
                        }
                        catch (Exception ex)
                        {
                            Form_message fm = new Form_message("数据库数据查找出错", "出错描述：" + ex.Message);
                            fm.ShowDialog();
                        }
                    }
                }
                this.Close();
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_MouseClick(object sender, MouseEventArgs e)
        {
            if (comboBox1.Text == "")
            {
                errorProvider2.SetError(comboBox1, "请先选择匹配对象");
            }
            else
            {
                errorProvider2.Clear();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBox1.Text == "age" || comboBox1.Text.EndsWith("ID"))
            {
                comboBox2.Items.Add("大于");
                comboBox2.Items.Add("小于");
            }
            else
            {
                if(comboBox2.Items.Contains("大于")|| comboBox2.Items.Contains("小于"))
                {
                    comboBox2.Items.Remove("大于");
                    comboBox2.Items.Remove("小于");
                }
            }
        }

        private void Form_Choose_SizeChanged(object sender, EventArgs e)
        {
            if (AutoSize != null)
            {
                AutoSize.FormSizeChanged();
            }
        }
    }
}
